import pygame
import pygame_menu as menu
from Map import Map,SCREEN_HEIGHT,SCREEN_WIDTH
from System import MainGame
from datetime import datetime
from Server import login,get_rank
import threading
import os
import pickle as pkl

MENU_WIDTH = 750
MENU_HEIGHT = 352

# 定义自己的主题
myimage = menu.baseimage.BaseImage(image_path='./img/mainbg2.jpg',drawing_mode=menu.baseimage.IMAGE_MODE_REPEAT_XY,drawing_offset=(0,0))
mytheme = menu.Theme()
mytheme.background_color = myimage

##
import tkinter as tk
from tkinter import filedialog
from PIL import Image
import numpy as np

def exitEvent(ret):
    if ret == 0:
        showGameWinMenu()
    if ret == 1:
        showGameOverMenu()
    if ret == 2:
        showGameExitMenu()

class ImageProcess():
    def __init__(self,image):
        super(ImageProcess, self).__init__()
        self.shape = (100,100)
        self.img = np.array(image.resize(self.shape))
        self.tankMask = np.zeros(self.shape,dtype=np.uint8)

    def Tank_Mask_process(self,):
        self.tankMask = np.zeros(self.shape,dtype=np.uint8)
        self.tankMask[int(self.shape[0]/5):,:] = 1
        self.tankMask[:,int(self.shape[0]/5*2):int(self.shape[0]/5*3)] = 1

        self.img = (self.img*(self.tankMask.reshape((self.shape[0],self.shape[1],1)))).astype(np.uint8)
        if self.img.shape[-1] == 3:
            print(self.img.shape)
            self.img = np.concatenate((self.img,255*self.tankMask.reshape(self.shape[0],self.shape[1],1)),axis=2)
            print(self.img.shape)
            return Image.fromarray(self.img,"RGBA")
        if self.img.shape[-1] == 4:
            return Image.fromarray(self.img, "RGBA")
        else:
            return None
        

def diyTank(path = "./img/generated_DIY_tank.png"):
    root=tk.Tk()
    root.withdraw()
    filePath=filedialog.askopenfilename()
    # if filePath[-3:]=='jpg':
    #     print('导入图片：',filePath)
    #     image = Image.open(filePath)
    #     image.save("./user_img/aaa.jpg")
    # if filePath[-3:]=='png':
    print('导入图片：',filePath)
    try:
        image = Image.open(filePath)
        image_process = ImageProcess(image)
        image = image_process.Tank_Mask_process()
        if image is None:
            return -1
        image.save(path)
        return 0
    except:
        return -1
def userInit():
    if Map.uid is None and Map.server:
        Map.server = False
        if not os.path.exists("user_cache"):
            os.mkdir("user_cache")
        if os.path.exists("user_cache/uid"):
            with open("user_cache/uid",'rb') as f:
                Map.uid = pkl.load(f)
        else:
            Map.uid = "user%s"%(str(round(hash(str(datetime.now()))%(1e7))))
            with open("user_cache/uid",'wb') as f:
                pkl.dump(Map.uid,f)
        login(Map.uid)
        print("success login is",Map.uid)
        Map.server = True


def showmainMenu():
    pygame.init()
    tLogin = threading.Thread(target = userInit,daemon=True)
    tLogin.start()
    tLogin.join(10)

    surface = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

    mainMenu = menu.Menu('Menu',
                         MENU_WIDTH,
                         MENU_HEIGHT,
                         theme=mytheme)

    def setmenu_adapter():
        showsetmenu(surface)

    def select_adapter():
        showSelectMenu(surface)

    def diymode_adapter():
        showDIYMenu(surface)
    def rank_adapter():
        showRankMenu(surface)
    myimage = menu.baseimage.BaseImage(image_path='./img/12.png', drawing_mode=menu.baseimage.IMAGE_MODE_FILL,
                                       drawing_offset=(0, 0))
    mainMenu.add.button('Main Game', select_adapter, background_color = myimage,selection_color=pygame.Color(0,0,0,0),font_color =pygame.Color(50,50,50,200),
                             align=menu.locals.ALIGN_LEFT)
    mainMenu.add.button('DIY mode', diymode_adapter, background_color = myimage,selection_color=pygame.Color(0,0,0,0),font_color =pygame.Color(50,50,50,200),
                             align=menu.locals.ALIGN_LEFT)
    mainMenu.add.button('Settings', setmenu_adapter, background_color = myimage,selection_color=pygame.Color(0,0,0,0),font_color =pygame.Color(50,50,50,200),
                             align=menu.locals.ALIGN_RIGHT)
    mainMenu.add.button('Rank', rank_adapter,
                             background_color = myimage,
                             selection_color=pygame.Color(0,0,0,0),font_color =pygame.Color(50,50,50,200),
                             align=menu.locals.ALIGN_RIGHT)
    mainMenu.add.button('Quit', menu.events.EXIT,
                             background_color = myimage,font_color =pygame.Color(50,50,50,200),
                             selection_color=pygame.Color(0,255,0),
                             align=menu.locals.ALIGN_RIGHT)
    label = mainMenu.add.label(f"uid:{Map.uid.replace('user','')}",font_size=17,font_color=(255,255,255))
    def setpos():
        # sl.set_position(100,100)
        # dy.set_position(600, 100)
        label.set_position(0,MENU_HEIGHT-30)
    mainMenu.mainloop(surface,setpos)

def showDIYMenu(surface):
    pygame.init()
    diymenu = menu.Menu('diy menu',
                        MENU_WIDTH,
                        MENU_HEIGHT,
                        theme=mytheme)

    def start_diy_map_adapter():
        MainGame(0).getDIYwall()
        showDIYOkMenu()

    def start_diy_tank_adapter():
        showDIYTankMenu(surface)

    def start_diy_enemy_adapter():
        showDIYEnemyMenu(surface)

    myimage = menu.baseimage.BaseImage(image_path='./img/12.png', drawing_mode=menu.baseimage.IMAGE_MODE_FILL,
                                       drawing_offset=(0, 0))
    diymenu.add.button('DIY tank', start_diy_tank_adapter, background_color = myimage,
                             selection_color=pygame.Color(0,0,0,0),font_color =pygame.Color(50,50,50,200),
                             align=menu.locals.ALIGN_CENTER) #modified
    diymenu.add.button('DIY Enemy', start_diy_enemy_adapter, background_color = myimage,
                             selection_color=pygame.Color(0,0,0,0),font_color =pygame.Color(50,50,50,200),
                             align=menu.locals.ALIGN_CENTER)
    diymenu.add.button('DIY map', start_diy_map_adapter, background_color = myimage,
                             selection_color=pygame.Color(0,0,0,0),font_color =pygame.Color(50,50,50,200),
                             align=menu.locals.ALIGN_CENTER)
    diymenu.add.button('Return', showmainMenu, background_color = myimage,
                             selection_color=pygame.Color(0,0,0,0),font_color =pygame.Color(50,50,50,200),
                             align=menu.locals.ALIGN_CENTER)
    label = diymenu.add.label(f"uid:{Map.uid.replace('user','')}",font_size=17,font_color=(255,255,255))
    def setpos():
        label.set_position(0,MENU_HEIGHT-30)
    diymenu.mainloop(surface,setpos)


def showSelectMenu(surface):
    pygame.init()


    selectMenu = menu.Menu('select menu',
                         MENU_WIDTH,
                         MENU_HEIGHT,
                         theme=mytheme)

    def start_the_game_level_1():
        exitEvent(MainGame(1).startGame())

    def start_the_game_level_2():
        exitEvent(MainGame(2).startGame())

    def start_the_game_level_3():
        exitEvent(MainGame(3).startGame())

    def start_the_game_diy():
        exitEvent(MainGame(0).startGame())

    myimage = menu.baseimage.BaseImage(image_path='./img/12.png', drawing_mode=menu.baseimage.IMAGE_MODE_FILL,
                                       drawing_offset=(0, 0))
    selectMenu.add.button('Desert', start_the_game_level_1, background_color = myimage,
                             selection_color=pygame.Color(0,0,0,0),font_color =pygame.Color(50,50,50,200),
                             align=menu.locals.ALIGN_LEFT)
    selectMenu.add.button('Valley', start_the_game_level_2, background_color = myimage,
                             selection_color=pygame.Color(0,0,0,0),font_color =pygame.Color(50,50,50,200),
                             align=menu.locals.ALIGN_RIGHT)
    selectMenu.add.button('Jungle', start_the_game_level_3, background_color = myimage,
                             selection_color=pygame.Color(0,0,0,0),font_color =pygame.Color(50,50,50,200),
                             align=menu.locals.ALIGN_LEFT)
    selectMenu.add.button('DIY', start_the_game_diy, background_color = myimage,
                             selection_color=pygame.Color(0,0,0,0),font_color =pygame.Color(50,50,50,200),
                             align=menu.locals.ALIGN_RIGHT)
    selectMenu.add.button('Return', selectMenu.disable, background_color = myimage,
                             selection_color=pygame.Color(0,0,0,0),font_color =pygame.Color(50,50,50,200),
                             align=menu.locals.ALIGN_CENTER)
    label = selectMenu.add.label(f"uid:{Map.uid.replace('user','')}",font_size=17,font_color=(255,255,255))
    def setpos():
        label.set_position(0,MENU_HEIGHT-30)

    selectMenu.mainloop(surface,setpos)


def showsetmenu(surface):
    pygame.init()
    settingMenu = menu.Menu('Setting',
                            MENU_WIDTH,
                            MENU_HEIGHT,
                            theme=mytheme)
    myimage = menu.baseimage.BaseImage(image_path='./img/mainbg3.png', drawing_mode=menu.baseimage.IMAGE_MODE_REPEAT_XY,
                                       drawing_offset=(-25,-23))
    backimage = menu.baseimage.BaseImage(image_path='./img/back.png', drawing_mode=menu.baseimage.IMAGE_MODE_FILL,
                                       drawing_offset=(0,0))
    newtheme = mytheme.copy()
    newtheme.background_color = myimage
    infoMenu = menu.Menu('Developer Info',
                         MENU_WIDTH,
                         MENU_HEIGHT,
                         theme=newtheme)

    DevelopInfo = '本游戏由“创意工坊”小组开发，' \
                  '最终解释权归“创意工坊”小组所有。\n' \
                  '开发者名单：郑文卓 冯靖皓 ' \
                  '郑煜 蒋铭阳 刘昊 王运泽' \
                  '\n源码见：https://gitee.com/DIY_battlefield/diy-battle'

    infoMenu.add.label(DevelopInfo, max_char=-1, font_name='华文楷体', font_size=20,font_color = pygame.Color(0,0,0))


    def volume_adapter(x):
        if x==0 or x==100:
            pass
        else:
            Map.volume = int(x)
        return str(int(x))
    myimage = menu.baseimage.BaseImage(image_path='./img/12.png', drawing_mode=menu.baseimage.IMAGE_MODE_FILL,
                                       drawing_offset=(0, 0))
    settingMenu.add.range_slider('Volume', Map.volume, (0, 100), 1,
                        rangeslider_id='range_slider',range_text_value_color=pygame.Color(255,255,255),
                                 range_box_color=pygame.Color(255,255,255),
                        value_format=volume_adapter,slider_selected_color=pygame.Color(255,255,255),
                                 background_color=myimage,
                                 selection_color=pygame.Color(0, 0, 0, 0), font_color=pygame.Color(50, 50, 50, 200),
                                 align=menu.locals.ALIGN_CENTER)
    settingMenu.add.button('Developer Info', infoMenu, background_color=myimage,
                                 selection_color=pygame.Color(0, 0, 0, 0), font_color=pygame.Color(50, 50, 50, 200),
                                 align=menu.locals.ALIGN_CENTER)
    settingMenu.add.button('Return', settingMenu.disable, background_color=myimage,
                                 selection_color=pygame.Color(0, 0, 0, 0), font_color=pygame.Color(50, 50, 50, 200),
                                 align=menu.locals.ALIGN_CENTER)
    label = settingMenu.add.label(f"uid:{Map.uid.replace('user','')}",font_size=17,font_color=(255,255,255))
    labelInfo = infoMenu.add.label(f"uid:{Map.uid.replace('user','')}",font_size=17,font_color=(255,255,255))
    def setpos():
        label.set_position(0,MENU_HEIGHT-30)
        labelInfo.set_position(0,MENU_HEIGHT-30)
    settingMenu.mainloop(surface,setpos)

def showGameOverMenu():
    pygame.init()
    surface = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
    def rank_adapter():
        showRankMenu(surface)
    gameoverMenu = menu.Menu('game over',
                         MENU_WIDTH,
                         MENU_HEIGHT,
                         theme=mytheme)
    myimage = menu.baseimage.BaseImage(image_path='./img/12.png', drawing_mode=menu.baseimage.IMAGE_MODE_FILL,
                                       drawing_offset=(0, 0))
    backimage = menu.baseimage.BaseImage(image_path='./img/13.png', drawing_mode=menu.baseimage.IMAGE_MODE_FILL,
                                       drawing_offset=(0, 0))
    gameoverMenu.add.label('Your Score: %d'%Map.grade,background_color=backimage,
                                 font_color=pygame.Color(255,255,255),
                                 align=menu.locals.ALIGN_CENTER)
    gameoverMenu.add.button('main menu',showmainMenu, background_color=myimage,
                                 selection_color=pygame.Color(0, 0, 0, 0), font_color=pygame.Color(50, 50, 50, 200),
                                 align=menu.locals.ALIGN_CENTER)
    gameoverMenu.add.button('Rank',rank_adapter, background_color=myimage,
                                 selection_color=pygame.Color(0, 0, 0, 0), font_color=pygame.Color(50, 50, 50, 200),
                                 align=menu.locals.ALIGN_CENTER)
    gameoverMenu.add.button('Quit',menu.events.EXIT, background_color=myimage,
                                 selection_color=pygame.Color(0, 0, 0, 0), font_color=pygame.Color(50, 50, 50, 200),
                                 align=menu.locals.ALIGN_CENTER)

    label = gameoverMenu.add.label(f"uid:{Map.uid.replace('user','')}",font_size=17,font_color=(255,255,255))
    def setpos():
        label.set_position(0,MENU_HEIGHT-30)
    gameoverMenu.mainloop(surface,setpos)

def showGameWinMenu():
    pygame.init()
    surface = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
    myimage = menu.baseimage.BaseImage(image_path='./img/12.png', drawing_mode=menu.baseimage.IMAGE_MODE_FILL,
                                       drawing_offset=(0, 0))

    def rank_adapter():
        showRankMenu(surface)
    gameWinMenu = menu.Menu('game win',
                         MENU_WIDTH,
                         MENU_HEIGHT,
                         theme=mytheme)
    backimage = menu.baseimage.BaseImage(image_path='./img/13.png', drawing_mode=menu.baseimage.IMAGE_MODE_FILL,
                                       drawing_offset=(0, 0))
    gameWinMenu.add.label('Your Score: %d'%Map.grade,background_color=backimage,
                                 font_color=pygame.Color(255,255,255),
                                 align=menu.locals.ALIGN_CENTER)
    gameWinMenu.add.button('main menu',showmainMenu, background_color=myimage,
                                 selection_color=pygame.Color(0, 0, 0, 0), font_color=pygame.Color(50, 50, 50, 200),
                                 align=menu.locals.ALIGN_CENTER)
    gameWinMenu.add.button('Rank',rank_adapter, background_color=myimage,
                                 selection_color=pygame.Color(0, 0, 0, 0), font_color=pygame.Color(50, 50, 50, 200),
                                 align=menu.locals.ALIGN_CENTER)
    gameWinMenu.add.button('Quit',menu.events.EXIT, background_color=myimage,
                                 selection_color=pygame.Color(0, 0, 0, 0), font_color=pygame.Color(50, 50, 50, 200),
                                 align=menu.locals.ALIGN_CENTER)
    label = gameWinMenu.add.label(f"uid:{Map.uid.replace('user','')}",font_size=17,font_color=(255,255,255))
    def setpos():
        label.set_position(0,MENU_HEIGHT-30)
    gameWinMenu.mainloop(surface,setpos)

def showGameExitMenu():
    pygame.init()
    surface = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
    myimage = menu.baseimage.BaseImage(image_path='./img/12.png', drawing_mode=menu.baseimage.IMAGE_MODE_FILL,
                                       drawing_offset=(0, 0))
    gameExitMenu = menu.Menu('game exit',
                         MENU_WIDTH,
                         MENU_HEIGHT,
                         theme=mytheme)
    backimage = menu.baseimage.BaseImage(image_path='./img/13.png', drawing_mode=menu.baseimage.IMAGE_MODE_FILL,
                                       drawing_offset=(0, 0))
    gameExitMenu.add.label('Exit will not update grade',background_color=backimage,
                                 font_color=pygame.Color(255,255,255),
                                 align=menu.locals.ALIGN_CENTER)
    gameExitMenu.add.button('main menu',showmainMenu, background_color=myimage,
                                 selection_color=pygame.Color(0, 0, 0, 0), font_color=pygame.Color(50, 50, 50, 200),
                                 align=menu.locals.ALIGN_CENTER)
    gameExitMenu.add.button('Quit',menu.events.EXIT, background_color=myimage,
                                 selection_color=pygame.Color(0, 0, 0, 0), font_color=pygame.Color(50, 50, 50, 200),
                                 align=menu.locals.ALIGN_CENTER)
    label = gameExitMenu.add.label(f"uid:{Map.uid.replace('user','')}",font_size=17,font_color=(255,255,255))
    def setpos():
        label.set_position(0,MENU_HEIGHT-30)
    gameExitMenu.mainloop(surface,setpos)

def showDIYOkMenu():
    pygame.init()
    surface = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

    DIYOkMenu = menu.Menu('DIY Complete!',
                         MENU_WIDTH,
                         MENU_HEIGHT,
                         theme=mytheme)
    def start_the_game_diy():
        exitEvent(MainGame(0).startGame())
    def diyMenuAdapter():
        showDIYMenu(surface)
    myimage = menu.baseimage.BaseImage(image_path='./img/12.png', drawing_mode=menu.baseimage.IMAGE_MODE_FILL,
                                       drawing_offset=(0, 0))
    DIYOkMenu.add.button('DIY Game', start_the_game_diy, background_color=myimage,
                                 selection_color=pygame.Color(0, 0, 0, 0), font_color=pygame.Color(50, 50, 50, 200),
                                 align=menu.locals.ALIGN_CENTER)
    DIYOkMenu.add.button('main menu',showmainMenu, background_color=myimage,
                                 selection_color=pygame.Color(0, 0, 0, 0), font_color=pygame.Color(50, 50, 50, 200),
                                 align=menu.locals.ALIGN_CENTER)
    DIYOkMenu.add.button('DIY menu',diyMenuAdapter, background_color=myimage,
                                 selection_color=pygame.Color(0, 0, 0, 0), font_color=pygame.Color(50, 50, 50, 200),
                                 align=menu.locals.ALIGN_CENTER)
    DIYOkMenu.add.button('Quit',menu.events.EXIT, background_color=myimage,
                                 selection_color=pygame.Color(0, 0, 0, 0), font_color=pygame.Color(50, 50, 50, 200),
                                 align=menu.locals.ALIGN_CENTER)
    label = DIYOkMenu.add.label(f"uid:{Map.uid.replace('user','')}",font_size=17,font_color=(255,255,255))
    def setpos():
        label.set_position(0,MENU_HEIGHT-30)
    DIYOkMenu.mainloop(surface,setpos)

def showRankMenu(surface):
    pygame.init()

    rankMenu = menu.Menu('Rank',
                         MENU_WIDTH,
                         MENU_HEIGHT,
                         theme=mytheme)
    if Map.server:
        rank = get_rank()
    else:
        rank = []
    FONT_SIZE = 18
    # rankMenu.add.label("#####################", font_color=pygame.color.Color(255, 255, 255),
    #                    background_color=pygame.Color(255, 255, 255, 100),
    #                    underline_color=pygame.color.Color(255, 255, 255),
    #                    font_size=FONT_SIZE)
    rankLeft = []
    rankRight = []
    rankLeft.append(rankMenu.add.label("UID", font_color=pygame.color.Color(255, 255, 255),
                                       underline_color=pygame.color.Color(255, 255, 255),
                                       font_size=FONT_SIZE))
    rankRight.append(rankMenu.add.label("Grade",font_color=pygame.color.Color(255, 255, 255),
                                        underline_color=pygame.color.Color(255, 255, 255),
                                        font_size=FONT_SIZE))
    for user in rank:
        rankLeft.append(rankMenu.add.label("%s"%user["uid"],font_color=pygame.color.Color(255, 255, 255),
                       underline_color=pygame.color.Color(255, 255, 255),
                       font_size=FONT_SIZE))
        rankRight.append(rankMenu.add.label("%s"%user['grade'],font_color=pygame.color.Color(255, 255, 255),
                       underline_color=pygame.color.Color(255, 255, 255),
                       font_size=FONT_SIZE))

    # rankMenu.add.label("#######################", font_color=pygame.color.Color(255, 255, 255),
    #                    background_color=pygame.Color(255, 255, 255, 100),
    #                    underline_color=pygame.color.Color(255, 255, 255),
    #                    font_size=FONT_SIZE)
    myimage = menu.baseimage.BaseImage(image_path='./img/12.png', drawing_mode=menu.baseimage.IMAGE_MODE_FILL,
                                       drawing_offset=(0, 0))
    button = rankMenu.add.button('Return',rankMenu.disable,font_color=pygame.color.Color(50, 50, 50, 200),
                                        selection_color=pygame.Color(0, 0, 0, 0),
                                       font_size=FONT_SIZE,background_color = myimage)
    label = rankMenu.add.label(f"uid:{Map.uid.replace('user','')}",font_size=FONT_SIZE,font_color=(255,255,255))
    font = label.get_font_info()
    font['font'] = font['name']
    font['font_size'] = 25
    font.pop('size')
    font.pop('name')
    def setpos():
        for i,(lw,rw) in enumerate(zip(rankLeft,rankRight)):
            lw.set_position(MENU_WIDTH//2-200,40+80*(i>0)+i*(MENU_HEIGHT-200)//len(rankLeft))
            lw.set_font(**font)
            rw.set_position(MENU_WIDTH//2+100,40+80*(i>0)+i*(MENU_HEIGHT-200)//len(rankLeft))
            rw.set_font(**font)
        label.set_position(0,MENU_HEIGHT-30)
        button.set_position(50,100)
    rankMenu.mainloop(surface,setpos)

def showDIYPlayer():
    pygame.init()
    surface = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

    DIYTankMenu = menu.Menu('DIYTank',
                         MENU_WIDTH,
                         MENU_HEIGHT,
                         theme=mytheme)
    myimage = menu.baseimage.BaseImage(image_path='./img/12.png', drawing_mode=menu.baseimage.IMAGE_MODE_FILL,
                                       drawing_offset=(0, 0))
    backimage = menu.baseimage.BaseImage(image_path='./img/13.png', drawing_mode=menu.baseimage.IMAGE_MODE_FILL,
                                       drawing_offset=(0, 0))
    DIYTankMenu.add.label("upload your tank",font_color = pygame.Color(255,255,255),
                                        selection_color=pygame.Color(0, 0, 0, 0),
                                       font_size=25,background_color = backimage)
    if not os.path.exists("./img/generated_DIY_tank.png"):
        DIYTankMenu.add.image("./img/invalid.jpg",scale=(0.3,0.3))
    else:
        DIYTankMenu.add.image("./img/generated_DIY_tank.png", scale=(1,1))
    widgetList = DIYTankMenu.get_widgets()
    label = widgetList[0]

    def start_the_game_diy():
        ret = diyTank(path = "./img/generated_DIY_tank.png")
        if ret == 0: # success
            im = DIYTankMenu.get_widgets()[1]
            DIYTankMenu.remove_widget(im)
            DIYTankMenu.add.image("./img/generated_DIY_tank.png")
            im = DIYTankMenu.get_widgets()[-1]
            DIYTankMenu.move_widget_index(im,1)
            label.set_title("upload success")
        else:
            label.set_title("upload failed, try again")
    DIYTankMenu.add.button('upload image',start_the_game_diy,font_color = pygame.Color(50, 50, 50, 200),
                                        selection_color=pygame.Color(0, 0, 0, 0),
                                       font_size=25,background_color = myimage)
    DIYTankMenu.add.button('Return',DIYTankMenu.disable,font_color = pygame.Color(50, 50, 50, 200),
                                        selection_color=pygame.Color(0, 0, 0, 0),
                                       font_size=25,background_color = myimage)
    label1 = DIYTankMenu.add.label(f"uid:{Map.uid.replace('user','')}",font_size=17,font_color=(255,255,255))
    def setpos():
        label1.set_position(0,MENU_HEIGHT-30)
    DIYTankMenu.mainloop(surface,setpos)

def showPlayerAttributeMenu(surface): #13
    pygame.init()

    settingMenu = menu.Menu('Setting Attributes',
                            MENU_WIDTH,
                            MENU_HEIGHT,
                            theme=mytheme)
    myimage = menu.baseimage.BaseImage(image_path='./img/12.png', drawing_mode=menu.baseimage.IMAGE_MODE_FILL,
                                       drawing_offset=(0, 0))
    def speed_adapter(x):
        # global tmp_value
        if x==0 or x==10:
            pass
        else:
            Map.player_attributes[0] = x
            print("change",Map.player_attributes)
        return str(int(x))

    def bullet_adapter(x):
        # global tmp_value
        if x==0 or x==10:
            pass
        else:
            Map.player_attributes[1] = x
            print("change",Map.player_attributes)
        return str(int(x))

    def shot_adapter(x):
        # global tmp_value
        if x==0 or x==10:
            pass
        else:
            Map.player_attributes[2] = x
            print("change",Map.player_attributes)
        return str(int(x))

    tmp_value = 0
    settingMenu.add.range_slider('Player speed', Map.player_attributes[0], (0, 10), 1,
                        rangeslider_id='range_slider1',
                        value_format=speed_adapter,font_color = pygame.Color(255,255,255),
                                        selection_color=pygame.Color(0, 0, 0, 0),
                                       background_color = myimage)
    settingMenu.add.range_slider('Player bullet speed', Map.player_attributes[1], (0, 10),1 ,
                        rangeslider_id='range_slider2',
                        value_format=bullet_adapter,font_color = pygame.Color(255,255,255),
                                        selection_color=pygame.Color(0, 0, 0, 0),
                                       background_color = myimage)
    settingMenu.add.range_slider('Player bullet capacity', Map.player_attributes[2], (0, 10), 1,
                        rangeslider_id='range_slider3',
                        value_format=shot_adapter,font_color = pygame.Color(255,255,255),
                                        selection_color=pygame.Color(0, 0, 0, 0),
                                       background_color = myimage)
    # settingMenu.add.button('Developer Info', infoMenu, background_color = pygame.Color(255,255,255,100),selection_color=pygame.Color(0,255,0))
    settingMenu.add.button('Return', settingMenu.disable, font_color = pygame.Color(50, 50, 50, 200),
                                        selection_color=pygame.Color(0, 0, 0, 0),
                                       background_color = myimage)
    label = settingMenu.add.label(f"uid:{Map.uid.replace('user','')}",font_size=17,font_color=(255,255,255))
    def setpos():
        label.set_position(0,MENU_HEIGHT-30)
    settingMenu.mainloop(surface,setpos)

def showDIYTankMenu(surface): #13
    pygame.init()
    diymenu = menu.Menu('diy tank menu',
                        MENU_WIDTH,
                        MENU_HEIGHT,
                        theme=mytheme)

    def start_diy_image_adapter():
        showDIYPlayer()

    def start_diy_attribute_adapter():
        showPlayerAttributeMenu(surface)
    myimage = menu.baseimage.BaseImage(image_path='./img/12.png', drawing_mode=menu.baseimage.IMAGE_MODE_FILL,
                                       drawing_offset=(0, 0))

    diymenu.add.button('DIY Image', start_diy_image_adapter, font_color = pygame.Color(50, 50, 50, 200),
                                        selection_color=pygame.Color(0, 0, 0, 0),
                                       background_color = myimage) #modified
    diymenu.add.button('DIY attribute', start_diy_attribute_adapter, font_color = pygame.Color(50, 50, 50, 200),
                                        selection_color=pygame.Color(0, 0, 0, 0),
                                       background_color = myimage)
    # diymenu.add.button('DIY enemy', start_diy_enemy_adapter, background_color = pygame.Color(255,255,255,100),selection_color=pygame.Color(0,255,0)) #13
    # diymenu.add.button('Return', showmainMenu, background_color = pygame.Color(255,255,255,100),selection_color=pygame.Color(0,255,0))
    diymenu.add.button('Return',diymenu.disable, font_color = pygame.Color(50, 50, 50, 200),
                                        selection_color=pygame.Color(0, 0, 0, 0),
                                       background_color = myimage)
    label1 = diymenu.add.label(f"uid:{Map.uid.replace('user','')}",font_size=17,font_color=(255,255,255))
    def setpos():
        label1.set_position(0,MENU_HEIGHT-30)
    diymenu.mainloop(surface,setpos)

def DIYEnemyImage():
    pygame.init()
    surface = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
    myimage = menu.baseimage.BaseImage(image_path='./img/12.png', drawing_mode=menu.baseimage.IMAGE_MODE_FILL,
                                       drawing_offset=(0, 0))
    backimage = menu.baseimage.BaseImage(image_path='./img/13.png', drawing_mode=menu.baseimage.IMAGE_MODE_FILL,
                                       drawing_offset=(0, 0))
    DIYTankMenu = menu.Menu('DIYEnemy',
                        MENU_WIDTH,
                        MENU_HEIGHT,
                        theme=mytheme)
    DIYTankMenu.add.label("upload enemy tank",font_color = pygame.Color(255,255,255),
                                        selection_color=pygame.Color(0, 0, 0, 0),
                                       font_size=25,background_color = backimage)
    if not os.path.exists("./img/generated_enemy_tank.png"):
        DIYTankMenu.add.image("./img/invalid.jpg",scale=(0.3,0.3))
    else:
        DIYTankMenu.add.image("./img/generated_enemy_tank.png", scale=(1,1))

    widgetList = DIYTankMenu.get_widgets()
    label = widgetList[0]

    def start_the_game_diy():
        ret = diyTank(path = "./img/generated_enemy_tank.png")
        if ret == 0: # success
            im = DIYTankMenu.get_widgets()[1]
            DIYTankMenu.remove_widget(im)
            DIYTankMenu.add.image("./img/generated_enemy_tank.png")
            im = DIYTankMenu.get_widgets()[-1]
            DIYTankMenu.move_widget_index(im,1)
            label.set_title("upload success")
        else:
            label.set_title("upload failed, try again")
    DIYTankMenu.add.button('upload image',start_the_game_diy,font_color = pygame.Color(50, 50, 50, 200),
                                        selection_color=pygame.Color(0, 0, 0, 0),
                                       background_color = myimage)
    DIYTankMenu.add.button('Return',DIYTankMenu.disable,font_color = pygame.Color(50, 50, 50, 200),
                                        selection_color=pygame.Color(0, 0, 0, 0),
                                       background_color = myimage)
    label1 = DIYTankMenu.add.label(f"uid:{Map.uid.replace('user','')}",font_size=17,font_color=(255,255,255))
    def setpos():
        label1.set_position(0,MENU_HEIGHT-30)
    DIYTankMenu.mainloop(surface,setpos)

def showAttributeMenu(surface):
    pygame.init()

    settingMenu = menu.Menu('Setting Attributes',
                            MENU_WIDTH,
                            MENU_HEIGHT,
                            theme=mytheme)
    myimage = menu.baseimage.BaseImage(image_path='./img/12.png', drawing_mode=menu.baseimage.IMAGE_MODE_FILL,
                                       drawing_offset=(0, 0))
    def speed_adapter(x):
        # global tmp_value
        if x==0 or x==10:
            pass
        else:
            try:
                    Map.enemy_attributes[0] = x
            except:
                Map.enemy_attributes = [3,6,5]
                Map.enemy_attributes[0] = x
            print("change",Map.enemy_attributes)
        return str(int(x))

    def bullet_adapter(x):
        # global tmp_value
        if x==0 or x==10:
            pass
        else:
            try:
                Map.enemy_attributes[1] = x
            except:
                Map.enemy_attributes = [3,6,5]
                Map.enemy_attributes[1] = x
            print("change",Map.enemy_attributes)
        return str(int(x))

    def shot_adapter(x):
        # global tmp_value
        if x==0 or x==10:
            pass
        else:
            try:
                Map.enemy_attributes[2] = x
            except:
                Map.enemy_attributes = [3,6,5]
                Map.enemy_attributes[2] = x
            print("change",Map.enemy_attributes)
        return str(int(x))

    tmp_value = 0
    settingMenu.add.range_slider('Enemy speed', Map.enemy_attributes[0], (0, 10), 1,
                        rangeslider_id='range_slider1',
                        value_format=speed_adapter,font_color = pygame.Color(255,255,255),
                                        selection_color=pygame.Color(0, 0, 0, 0),
                                       background_color = myimage)
    settingMenu.add.range_slider('Enemy bullet speed', Map.enemy_attributes[1], (0, 10), 1,
                        rangeslider_id='range_slider2',
                        value_format=bullet_adapter,font_color = pygame.Color(255,255,255),
                                        selection_color=pygame.Color(0, 0, 0, 0),
                                       background_color = myimage)
    settingMenu.add.range_slider('Enemy shot frequency', Map.enemy_attributes[2], (0, 10), 1,
                        rangeslider_id='range_slider3',
                        value_format=shot_adapter,font_color = pygame.Color(255,255,255),
                                        selection_color=pygame.Color(0, 0, 0, 0),
                                       background_color = myimage)
    # settingMenu.add.button('Developer Info', infoMenu, background_color = pygame.Color(255,255,255,100),selection_color=pygame.Color(0,255,0))
    settingMenu.add.button('Return', settingMenu.disable, font_color = pygame.Color(50, 50, 50, 200),
                                        selection_color=pygame.Color(0, 0, 0, 0),
                                       background_color = myimage)
    label = settingMenu.add.label(f"uid:{Map.uid.replace('user','')}",font_size=17,font_color=(255,255,255))
    def setpos():
        label.set_position(0,MENU_HEIGHT-30)
    settingMenu.mainloop(surface,setpos)

def showDIYEnemyMenu(surface): #13
    pygame.init()
    diymenu = menu.Menu('diy enemy menu',
                        MENU_WIDTH,
                        MENU_HEIGHT,
                        theme=mytheme)

    def start_diy_image_adapter():
        DIYEnemyImage()

    def start_diy_attribute_adapter():
        showAttributeMenu(surface)
    myimage = menu.baseimage.BaseImage(image_path='./img/12.png', drawing_mode=menu.baseimage.IMAGE_MODE_FILL,
                                       drawing_offset=(0, 0))
    diymenu.add.button('DIY Image', start_diy_image_adapter, font_color = pygame.Color(50, 50, 50, 200),
                                        selection_color=pygame.Color(0, 0, 0, 0),
                                       background_color = myimage)
    diymenu.add.button('DIY attribute', start_diy_attribute_adapter, font_color = pygame.Color(50, 50, 50, 200),
                                        selection_color=pygame.Color(0, 0, 0, 0),
                                       background_color = myimage)

    diymenu.add.button('Return',diymenu.disable, font_color = pygame.Color(50, 50, 50, 200),
                                        selection_color=pygame.Color(0, 0, 0, 0),
                                       background_color = myimage)
    label = diymenu.add.label(f"uid:{Map.uid.replace('user','')}",font_size=17,font_color=(255,255,255))
    def setpos():
        label.set_position(0,MENU_HEIGHT-30)
    diymenu.mainloop(surface,setpos)
